<?php 
class ContentTag{
	
	public $tag=array(
		'arclist'=>array('block'=>1,'level'=>4),
		'channel'=>array('block'=>1,'level'=>4),
		'pagelist'=>array('block'=>1,'level'=>4),
		'pageshow'=>array('block'=>0,'level'=>4)

	);

	public function _arclist($attr,$content){
		$cid=isset($attr['cid'])?$attr['cid']:'';
		$aid=isset($attr['aid'])?$attr['aid']:'';
		$row=isset($attr['row'])?$attr['row']:'10';
		$titlelen=isset($attr['titlelen'])?$attr['titlelen']:'20';

		$php=<<<php
		<?php
		\$cid='$cid';
		\$aid='$aid';
		\$db = K('Content');
		if(\$cid)\$db->where("cc.cid IN(". \$cid.")");
		if(\$aid)\$db->where("aid in(\$aid)");
		\$data = \$db->where(\$where)->limit($row)->all();		
		foreach(\$data as \$field){
			\$field['url']=getContentUrl(\$field);
			\$field['title']=mb_substr(\$field['title'],0,$titlelen,'utf-8');
		?>

php;
		$php.=$content;
		$php.="<?php } ;?>";
		return $php;
	}

	//读取栏目
	public function _channel($attr,$content){
		$cid=isset($attr['cid'])?$attr['cid']:'';
		$type=isset($attr['type'])?$attr['type']:'self';

		$php=<<<php
		<?php
		\$cid=$cid;
		\$type=$type;
		\$db=M('category');
		switch (\$type) {
			case 'self':
				\$cat=\$db->find(\$cid);
				p(\$cat);exit;
				\$data=\$db->where("pid=".\$cat['pid'])->all();						
				break;
			
			default:
				\$data=\$db->where("pid=".\$cid)->all();
				break;
		}
		foreach (\$data as \$field) {
			\$field['caturl']=getCategoryUrl(\$field);
		?>
		
php;
		$php.=$content;
		$php.="<?php };?>";
		return $php;
	}
	//分页列表数据
	public function _pagelist($attr,$content){
		$row=isset($attr['row'])?$attr['row']:'5';
		$titlelen=isset($attr['titlelen'])?$attr['titlelen']:'20';

		$php=<<<php
		<?php
		\$cid=\$_GET['cid'];
		//获取所有子栏目数据
		\$cat=Data::channelList(M('category')->all,\$cid);
		//获取所以子栏目的cid
		\$cat=array_keys(\$cat);
		\$cat[]=\$cid;	
		\$db=K('Content');
		\$where=\$db->where("cc.cid IN(".implode(',',\$cat).")");
		\$page=new Page(\$db->where(\$where)->count(),$row);
		\$data=\$db->where(\$where)->limit(\$page->limit())->all();
		foreach (\$data as \$field) {
			\$field['url']=getContentUrl(\$field);
			\$field['title']=mb_substr(\$field['title'],0,$titlelen,'utf-8');

		?>
php;
		$php.=$content;
		$php.="<?php };?>";
		return $php;
	}

	public function _pageshow($attr,$content){
		return "<?php echo \$page->show() ;?>";
	}


}


?>